package evi.com.evi.adapter;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import evi.com.evi.R;
import evi.com.evi.activity.GoodsSortWriteActivity;
import evi.com.evi.activity.GoodsUpdateActivity;
import evi.com.evi.bean.ClassifyBean;
import evi.com.evi.utils.PopStringList;

public class GoodsSortAdapter<T> extends BaseAdapter {

    private Context mContext;
    private List<T> mList;
    private List<String> sizeList;

    public GoodsSortAdapter(Context context, List<T> mList, List<String> sizeList) {
        this.mContext = context;
        this.mList = mList;
        this.sizeList = sizeList;
        if (this.mList == null) {
            this.mList = new ArrayList<T>();
        }
        if (this.sizeList == null) {
            this.sizeList = new ArrayList<>();
        }
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_lv_goods_sort_write, null);

            viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
            viewHolder.iv_write = (ImageView) convertView.findViewById(R.id.iv_write);
            viewHolder.iv_delete = (ImageView) convertView.findViewById(R.id.iv_delete);
            viewHolder.iv_move = (ImageView) convertView.findViewById(R.id.iv_move);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        if (position == 0) {
            viewHolder.iv_move.setVisibility(View.INVISIBLE);
        } else {
            viewHolder.iv_move.setVisibility(View.VISIBLE);
        }

        ClassifyBean bean = (ClassifyBean) mList.get(position);
        viewHolder.tv_name.setText(bean.getName());
        viewHolder.iv_write.setOnClickListener(new OnitemListener(position, mList.get(position), null));
        viewHolder.iv_delete.setOnClickListener(new OnitemListener(position, mList.get(position), sizeList.get(position)));
        viewHolder.iv_move.setOnClickListener(new OnitemListener(position, mList.get(position), null));

        return convertView;
    }

    class OnitemListener implements OnClickListener {
        private int position;
        private Object obj;
        private String size;

        public OnitemListener(int position, Object obj, String size) {
            this.position = position;
            this.obj = obj;
            this.size = size;
        }

        @Override
        public void onClick(View v) {
            ClassifyBean info = (ClassifyBean) obj;
            switch (v.getId()) {
                case R.id.iv_write:        //编辑
                    Intent intent = new Intent(mContext, GoodsUpdateActivity.class);
                    intent.putExtra("ActType", "修改分类");
                    intent.putExtra("name", info.getName());
                    intent.putExtra("productCategoryId", info.getProductCategoryId());
                    ((GoodsSortWriteActivity) mContext).startActivityForResult(intent, 1);
                    break;

                case R.id.iv_delete:        //删除
                    Log.e("TAG", "size" + size);

                    if ("0".equals(size)) {

                        String productCategoryId = info.getProductCategoryId();
                        ((GoodsSortWriteActivity) mContext).deleteSort(productCategoryId, null);
                    } else {
                        List<ClassifyBean> tempList = new ArrayList<>();
                        tempList.addAll((Collection<? extends ClassifyBean>) mList);
                        ClassifyBean tempBean = null;
                        for (int i = 0; i < tempList.size(); i++) {
                            ClassifyBean bean = tempList.get(i);
                            if (bean.getProductCategoryId().equals(info.getProductCategoryId())) {
                                tempBean = bean;
                            }
                        }
                        tempList.remove(tempBean);

                        String[] id = new String[mList.size() - 1];
                        String[] content = new String[mList.size() - 1];
                        for (int i = 0; i < tempList.size(); i++) {
                            id[i] = tempList.get(i).getProductCategoryId();
                            content[i] = tempList.get(i).getName();
                        }

                        final String productCategoryId = info.getProductCategoryId();
                        new PopStringList().getStringlist((GoodsSortWriteActivity) mContext, "请选择转移商品的分类名", content, id, new PopStringList.Stringcallback() {
                            @Override
                            public void sure(String chosed, String id) {
                                ((GoodsSortWriteActivity) mContext).deleteSort(productCategoryId, id);
                            }
                        });
                    }


                    break;

                case R.id.iv_move:        //移动
                    ClassifyBean newBean = (ClassifyBean) mList.get(position - 1);
                    ((GoodsSortWriteActivity) mContext).moveSort(info.getProductCategoryId(), newBean.getProductCategoryId());
                    break;
                default:
                    break;
            }
        }

    }

    class ViewHolder {
        TextView tv_name;
        ImageView iv_write;
        ImageView iv_delete;
        ImageView iv_move;
    }

}
